﻿// License: The MIT License (MIT) Copyright (c) 2011..2012 Barend Gehrels

// Permission is hereby granted, free of charge, to any person obtaining a
// copy of this software and associated documentation files (the "Software"),
// to deal in the Software without restriction, including without limitation
// the rights to use, copy, modify, merge, publish, distribute, sublicense,
// and/or sell copies of the Software, and to permit persons to whom the
// Software is furnished to do so, subject to the following conditions:

// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.

// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
// FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
// DEALINGS IN THE SOFTWARE.

using System;
using System.IO;
using System.Windows;

// http://www.chartlyrics.com/api.aspx


// Added logging, using:
// [1] http://www.beefycode.com/post/Log4Net-Tutorial-pt-1-Getting-Started.aspx
// [2] http://stackoverflow.com/questions/2539869/log4net-in-app-object
// [3] http://www.codeproject.com/KB/dotnet/Log4net_Tutorial.aspx
// [4] http://mitch-wheat.blogspot.com/2007/04/log4net-net-logging-tool.html
// Where [3] was the only to indicate next line which is necessary (at least for me)
// And [2] indicates that it can be global (though there are arguments for local)
// And [1] is the best tutorial I found
// And [4] gives a clear solution of how to do Log4Net.config

[assembly: log4net.Config.XmlConfigurator(ConfigFile = "Log4Net.config", Watch = true)]

namespace GrooveSharp
{
    public partial class App : Application
    {
        private static readonly log4net.ILog m_Logger;

        static App()
        {
            log4net.Config.XmlConfigurator.Configure();
            m_Logger = log4net.LogManager.GetLogger(typeof(App));
        }

        // Define own startup to parse command line:
        // http://stackoverflow.com/questions/426421/wpf-command-line
        protected override void OnStartup(StartupEventArgs e)
        {
            base.OnStartup(e);

            // If there something on the command line
            if (e.Args.Length > 1)
            {
                for (int i = 0; i < e.Args.Length; i++)
                {
                    // Check if it is the user to simulate
                    if (e.Args[i] == "--u" && i + 1 < e.Args.Length)
                    {
                        Db.UserName = e.Args[i + 1];
                    }
                }
            }

            var window = new GrooveSharpMainWindow();

            Microsoft.Win32.SystemEvents.PowerModeChanged += delegate(object o, Microsoft.Win32.PowerModeChangedEventArgs args)
            {
                window.SetPowerMode(args.Mode);
            };

            window.ShowDialog();
            this.Shutdown();
        }

        public static log4net.ILog Log
        {
            get { return m_Logger; }
        }

        public static void ReportException(Exception exc)
        {
            Log.Error(exc.Message, exc);
        }

        private void Application_DispatcherUnhandledException(object sender, System.Windows.Threading.DispatcherUnhandledExceptionEventArgs e)
        {
            ReportException(e.Exception);
            e.Handled = true;
        }

    }
}



